/*!
 * Comment block
 * usage:
 * $('.show_comment').aywComment({});
 *
 * Depends:
 *  jquery.ui.widget.js
 */
(function($, undefined) {
	$.widget('ui.aywComment', {
		options: {
			addUrl : '',
			delUrl : '',
		},
		_list: null,
		_data: 0,
		_loadUrl: U('home/Comment/getComment'),
		
		_create: function() {
			var self = this, data = $.parseJSON($(self.element).attr('data'));
			self._data = data;
			self._list = $('#comment_list_' + data.id);
			self._loadUrl = $(self.element).attr('href');
			if (!self._list.length) return false;
			
			$(self.element).click(function(){
				if ($(this).data('list')) {
					if ($(this).data('list').is(':visible')) {
						$(this).data('list').slideUp();
					} else {
						$(this).data('list').slideDown();
					}
				} else {
					$(self._list).html('<li class="loading">loading....</li>');
					self._loadComment(this);
				}
				
				return false;
			});
			
		},
		
		_initActions: function() {
			var self = this;
			$('.commenter', self._list).click(function(){
				$(this).hide();
				$(this).next('form.hidden').show().find('textarea').focus();
			});
			
			// commit comment action
			$('form',self._list).submit(function(){
				self._doAddComment();
				return false;
			});
			
			// init list actions
			self._initListActions();
		},
		
		_initListActions: function() {
			var self = this;
			$('.comment', self._list).unbind('hover').hover(function(){
				//$('.buttons', this).show();
				//$(this).css({backgroundColor:'#eee'});
			}, function() {
				//$('.buttons', this).hide();
				//$(this).css({backgroundColor:''});
			});
			
			// avatar init
			$('.avatar', self._list).aywFriend();
			
			//reply
			$('.reply', self._list).unbind('click').bind('click', function(){
				$('.commenter', self._list).click();
				var _name = $(this).attr('href');
				var textArea = $('.comment_content', self._list).val('回复'+_name+':').focus().get(0);
				var strlength = textArea.value.length;				
				if (document.selection) { //IE
					 var rng = textArea.createTextRange();
					 rng.collapse(true);
					 rng.moveStart("character",strlength)
				}else if (textArea.selectionStart || (textArea.selectionStart == '0')) { // Mozilla/Netscape…
			        textArea.selectionStart = strlength;
			        textArea.selectionEnd = strlength;
			    }
			    return false;
			});
			
			// buttons
			$('.ding,.cai,.ok,.delete', self._list).unbind('click').bind('click', function(){
				var _href = $(this).attr('href'), _self = this;
				$.get(_href,{},function(txt){
					if( txt == '1' ){
						if ($(_self).hasClass('delete')) {
							$(_self).closest('.comment_line').fadeOut(function(){
								$(this).remove();
								self._subNum(1);
							});
							ui.success('删除成功！');
						} else if ($(_self).hasClass('ding') || $(_self).hasClass('cai')) {
							var _data = $(_self).text().split('+');
							if (_data[1]) {
								_num = parseInt(_data[1])+1;
								$(_self).text(_data[0] + '+' + _num);
							}
						} else if ($(_self).hasClass('ok')) {
							$(_self).remove();
							ui.success('已采纳为最佳答案！');
						}
					} else {
						ui.error('操作失败');
					}
				});
				return false;
			});
		},
			
		checkCommentLength: function(obj, num){
	  	 	var $obj = $(obj);
			var str  = $obj.val();  
			var len  = getLength(str);
			if( len > num ){
				$obj.val(subStr(str, num));
			}
		},
		
		//获取评论列表
		_loadComment: function (handler){
			var self = this;
			$.getJSON(self._loadUrl,{},function(txt){
				if( txt.data ){
					self._list.hide().html(txt.data).slideDown();;
					self._initActions();
					$(handler).data('list', self._list);
				} else {
					ui.error('load comment error');
				}
			});
		},

		//添加评论
		_doAddComment: function (){
			var self = this, _editor =  $('.comment_content', self._list);
			var content = _editor.val();
			if( $.trim(content) =='' ){
				ui.error("请输入评论内容");
				$('textarea', self._list).focus();
				return false;
			}
			
			var appid = self._data.appid,
			    to_id = self._data.id,
			    type = self._data.type,
			    table = self._data.table,
			    id_field = self._data.id_field,
			    comment_count_field = self._data.comment_count_field
			    ;
			$.getJSON(U('home/Comment/doAddComment'),{comment:content,with_new_weibo:0,type:type,appid:appid,table:table, to_id:to_id, id_field:id_field, comment_count_field:comment_count_field},function(data){
				var _obj = $(data.data);
				$('.add_comment', self._list).after(_obj);
				self._subNum(0);
				self._initListActions();
				_editor.val('');
			});
		},
		
		_subNum: function(sub) {
			var self = this;
			if (sub) {
				var _num = parseInt($('.num', self.element).text())-1;
			} else {
				var _num = parseInt($('.num', self.element).text())+1;
			}
			$('.num', self.element).text(_num);
		}
		

	
	});
})(jQuery);
